<template>
  <div class="xtx-form">
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <input class="input" type="text" placeholder="请输入用户名" v-model="form.account" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input class="input" type="text" placeholder="请输入手机号" v-model="form.mobile" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input class="input" type="text" placeholder="请输入验证码"  v-model="form.code"/>
        <span class="code" @click="sendCode">
          <span>{{countTime===0?'发送验证码':`${countTime}秒后尝试`}}</span>
          </span>
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input class="input" type="password" placeholder="请输入密码" v-model="form.password" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input class="input" type="password" placeholder="请确认密码" v-model="form.rePassword"/>
      </div>
      <div class="error"></div>
    </div>
    <a href="javascript:;" class="submit" @click="submit()">立即提交</a>
  </div>
</template>

<script>
import { reactive } from 'vue'
import { message } from '@/components/Message'
import { reqUserQQPatchCode, reqUserQQPatchLogin } from '@/api/user'
import { useInterval } from '@/compositions/interval'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
export default {
  name: 'CallbackPatch',
  props: {
    unionId: {
      typr: String,
      default: ''
    }
  },
  setup (props) {
    const form = reactive({
      mobile: '',
      code: '',
      account: '',
      password: '',
      rePassword: ''
    })
    // 倒计时函数
    const { start, countTime } = useInterval(60)
    // 获取验证码
    const sendCode = async () => {
      // 用正则表达式校验手机号码
      if (!/^1[3-9]\d{9}$/.test(form.mobile)) return message({ text: '手机号码格式错误' })
      // const { result } = await reqUserQQPatchCode(form.mobile)
      // //console.log('获取回来的手机校验数据', result)
      // 发送请求获取验证码
      await reqUserQQPatchCode(form.mobile)
      // 调用倒计时计时的开始函数
      start()
    }
    const store = useStore()
    const router = useRouter()
    //  提交注册
    const submit = async () => {
      // 校验密码和确认密码
      if (form.password !== form.rePassword) return message({ text: '密码格式错误' })
      // 发送请求
      const { result } = await reqUserQQPatchLogin({
        // 展开原数组把unionId追加进去 把 unionId 追加到表单数据中
        unionId: props.unionId,
        ...form
      })
      // //console.log(result)
      // result 储存用户的信息，可以直接储存到vuex中
      store.commit('user/setProfile', result)
      // 完善成功跳转页面
      router.push('/')
      // 提示用户
      message({ type: 'success', text: '完善资料成功' })
    }
    return { sendCode, form, start, countTime, submit }
  }
}
</script>

<style scoped lang='less'>
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
